package com.example.hp.fragmenttest_322jzy.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hp.fragmenttest_322jzy.R;
import com.example.hp.fragmenttest_322jzy.adapter.MyAdapter;
import com.example.hp.fragmenttest_322jzy.bean.ArticleBean;
import com.example.hp.fragmenttest_322jzy.iface.ArticleListener;
import com.example.hp.fragmenttest_322jzy.model.ArticleModel;

import java.util.List;



/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment1 extends Fragment {
    private  TextView tvinfo;
    private View view=null;
    private Context context;
//粘贴
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示布局效果
    private MyAdapter tcareAdapter;//适配器
    private List<ArticleBean> list=null;//数据源
    ArticleListener tcareListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanlist) {
            list=beanlist;
            tcareAdapter.setList(list);//得到网书中的数据 传到adapter
            //通知的话 数据更新
            tcareAdapter.notifyDataSetChanged();
        }


        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "msg", Toast.LENGTH_SHORT).show();
        }
    };

//这里截至
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        ArticleModel tcareModel = new ArticleModel();
        //动态加载Fragment1的布局文件
        view = inflater.inflate(R.layout.fragment1, container, false);
        //获取布局上的控件，注意通过view获取
        tvinfo = (TextView) view.findViewById(R.id.textView8);

        //tvinfo.setText("微信");

        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载Fragment微信", Toast.LENGTH_SHORT).show();
        //返回动态生成的view
        //粘
        initRecyclerView();


        //调用哪个方法 拿到数据
        //第二页
        //在这使用它 实例化Artivitymodel 调用方法获取网络数据

        tcareModel.getResoutList("article", 2, "rv1l2tidbomann2qn86nln6ur2", tcareListener);
//这里
        return view;
    }
        //粘贴
        private void initRecyclerView() {
            //获取RecyclerView 设置属性 获取数据源 绑定
            recyclerView= (RecyclerView)view.findViewById(R.id.recyclerview);
            //创建默认的线性布局
            layoutManager=new LinearLayoutManager(context);
            //设置布局管理器
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setHasFixedSize(true);
            //  list=getData();
            tcareAdapter=new MyAdapter(context);
            tcareAdapter.setList(list);
            recyclerView.setAdapter(tcareAdapter);
        }
        //这里
    }

